/**
 * \class Enviroment.cpp
 *
 *  Created on: 13 apr 2011
 *      Author: ytc0039
 */

#include "Enviroment.h"
#include <Generator/context/EnvContext.h>
#include "CompoundContext.h"
#include "ExpressionContext.h"


Enviroment::Enviroment(): m_Context(NULL) {

}

Enviroment::~Enviroment() {

	/**
	 * / \todo Needs to remove the parent contexts too.
	 * right now this is a memory leak
	 */

	IContext* current;
	do
	{
		current = removeContext();
	}while(current->getParent() != NULL);

	//The last thing we do is remove the
	// EnvContext (Environment context)


	delete m_Context;
	m_Context = NULL;

}

IContext* Enviroment::getContextInstance()
{

	if( m_Context == NULL )
	{
		m_Context = new EnvContext();
	}

	return m_Context;
}



IContext* Enviroment::addCompoundContext()
{
	m_Context = new CompoundContext(m_Context);

	return m_Context;

}

IContext* Enviroment::addExpressionContext()
{
	m_Context = new ExpressionContext(m_Context);

	return m_Context;
}



IContext *Enviroment::removeContext()
{
	IContext* parentContext = m_Context->getParent();

	// In case the parent context is NULL, then its a enviroment context,
	// which means it should always be there.
	// if the parent context != NULL its a Decoration context,
	// and the current context should be removed. and the parent context
	// becomes the new context.
	if( parentContext != NULL)
	{
		delete m_Context;
		m_Context = parentContext;
	}

	return m_Context;

}








